package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.entity.Comment;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.ReplyList;
import com.example.xpvideo.entity.req.SecondReplyList;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.GoodsCommentsRes;
import com.example.xpvideo.entity.res.SecondComments;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.CommentMapper;
import com.example.xpvideo.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 评论表(Comment)Service层
 *
 * @author 杨丽辉
 * @since 2021-08-18 13:55:54
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class CommentServiceImpl implements CommentService {
        @Autowired
        private CommentMapper commentMapper;

    @Override
    public ResponseResult<PageResult<List<GoodsCommentsRes>>> replyList(ReplyList replyList) throws Exception {
        Integer page = replyList.getPage();
        Integer size = replyList.getSize();
        String targetId = replyList.getTargetId();

        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 10;
        }

        PageInfo<GoodsCommentsRes> objectPageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> commentMapper.commentListByTarId(Wrappers.<Comment>query().eq("c.goods_id",
                                targetId)
                        .eq("c .comment_level", 1)
                        .eq("c.deleted",0)
                        .orderByAsc( "c.create_time")));

        List<GoodsCommentsRes> commentListByTarIdList = objectPageInfo.getList();
        //当前页
        int pageNum = objectPageInfo.getPageNum();
        //总页数
        int pages = objectPageInfo.getPages();
        //当前页展示的数量
        int pageSize = objectPageInfo.getSize();
        //查询总数
        long count = objectPageInfo.getTotal();
        PageResult<List<GoodsCommentsRes
                >> listPageResult = new PageResult<>(count, pages, pageSize, pageNum,
                commentListByTarIdList);

        return ResponseResult.responseResult(0, listPageResult);
    }

    @Override
    public ResponseResult<PageResult<List<SecondComments>>> secondReplyList(SecondReplyList secondReplyList) throws Exception {
        Integer page = secondReplyList.getPage();
        Integer size = secondReplyList.getSize();
        String parentCommentId = secondReplyList.getParentCommentId();
        Long tokenId = null;

        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 10;
        }
        PageInfo<SecondComments> objectPageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> commentMapper.secondCommentList(Wrappers.<Comment>query()
                        .eq("c.parent_comment_id", parentCommentId)
                        .eq("c.comment_level", 2)
                        .eq("c.status", 10)
                        .eq("c.deleted",0)
                        .orderByAsc("c.create_time")));

        List<SecondComments> commentListByTarIdList = objectPageInfo.getList();
        //当前页
        int pageNum = objectPageInfo.getPageNum();
        //总页数
        int pages = objectPageInfo.getPages();
        //当前页展示的数量
        int pageSize = objectPageInfo.getSize();
        //查询总数
        long count = objectPageInfo.getTotal();
        PageResult<List<SecondComments>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum,
                commentListByTarIdList);

        return ResponseResult.responseResult(0, listPageResult);
    }

    @Override
    public ResponseResult<Object> deleteComment(UpdateReq updateReq) throws ErrorException {
        int res = commentMapper.update(null,Wrappers.<Comment>update()
                .set("deleted",1)
                .eq("id",updateReq.getId()));

        if(res > 0){
            return ResponseResult.responseResult(0, "删除评论成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20001);
    }
}

